package com.yulongtian.oneProblemEveryDay.month202301;

/**
 * https://leetcode.cn/problems/merge-in-between-linked-lists/
 * 可
 *
 * @author yulongTian
 * @create 2023-01-30 22:38
 */
public class Test21 {
    public static void main(String[] args) {

    }

    public static ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        a -= 1;
        ListNode point = list1;
        int index = 0;
        ListNode p2 = list2;
        while (p2 != null) {
            if (p2.next == null) {
                break;
            }
            p2 = p2.next;
        }
        while (true) {
            if (index == a) {
                ListNode temp = point;
                point = point.next;
                index++;
                temp.next = list2;
                continue;
            }
            if (index == b) {
                if (p2 != null) {
                    p2.next = point.next;
                }
                break;
            }

            point = point.next;
            index++;
        }

        return list1;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}
